﻿using DevExpress.Utils;
using DevExpress.XtraTab;
using System;
using System.Windows.Forms;

namespace CXW.Core.Sharp.Gui
{
    /// <summary>自定义 XtraTabPage 控件</summary>
    public class XtraTabPageExt : XtraTabPage
    {
        SuperToolTip superToolTip = new SuperToolTip();
        ToolTipItem toolTipItem = new ToolTipItem();

        /// <summary>显示内容根据此属性获取资源文件</summary>
        public string Language { get; private set; }

        /// <summary>该 显示UI控件 对应的 IViewContent 对象</summary>
        public IViewContent ViewContent { get; private set; }

        /// <summary>构造函数</summary>
        public XtraTabPageExt(IViewContent viewContent)
        {
            ViewContent = viewContent;
        }

        Control _control { get; set; }
        /// <summary>获取或设置包含在控件内部的控件,ViewContent 对象如果存在必须首先赋值</summary>
        public Control Control
        {
            get { return _control; }
            set
            {
                if (value != null)
                {
                    if (_control != value) { Controls.Remove(_control); }
                    _control = value;
                    if (_control is Form)
                    {
                        (_control as Form).Dock = DockStyle.Fill;
                        (_control as Form).TopLevel = false;
                        (_control as Form).FormBorderStyle = FormBorderStyle.None;
                        (_control as Form).Text = ViewContent == null ? Text : ViewContent.Label;
                        (_control as Form).Name = ViewContent == null ? Name : ViewContent.CodonId;
                        (_control as Form).Show();
                    }
                    else if (_control is Control)
                    {
                        (_control as Control).Dock = DockStyle.Fill;
                        (_control as Control).Text = ViewContent == null ? Text : ViewContent.Label;
                        (_control as Control).Name = ViewContent == null ? Name : ViewContent.CodonId;
                    }
                    else
                    {
                        throw new Exception("IViewContent.Control is not Windows.Forms or Windows.Control.");
                    }

                    Controls.Add(_control);

                    superToolTip.Items.Add(toolTipItem);
                    SuperTip = superToolTip;

                    Language = ViewContent.Language;
                    Name = ViewContent == null ? Name : ViewContent.CodonId;

                    ChangePageTitle();
                }
                else { throw new Exception("Control is Empty."); }
            }
        }

        string ResourceServiceLanguage = "";
        /// <summary>变更 XtraTabPageExt 标题及 ToolTip</summary>
        public void ChangePageTitle()
        {
            if (ResourceServiceLanguage != ResourceService.Language)
            {
                LoggingService.DebugFormatted("1 Name = {2},Text = {0},ToolTip = {1}", Text, toolTipItem.Text, Name);
                ResourceServiceLanguage = ResourceService.Language;
                Text = ViewContent == null ? Text : LanguageHelper.GetText(ViewContent);
                toolTipItem.Text = ViewContent == null ? toolTipItem.Text : ResourceService.GetString(ViewContent.InfoTip);
                LoggingService.DebugFormatted("--2 Name = {2},Text = {0},ToolTip = {1}", Text, toolTipItem.Text, Name);
            }
        }

        /// <remark/>
        public override string ToString()
        {
            return string.Format("BarSubItemExt Name = {0},Text = {1}", Name, Text);
        }
    }
}
